import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import http from '@/static/js/http';

const store = new Vuex.Store({
	modules:{
		
	},
	state: {
		user_info: uni.getStorageSync('userInfo') || null,
		vip_info: uni.getStorageSync('vipInfo') || null,
		user_property: {},
		access_token: uni.getStorageSync('access_token'),
		current_city: {},
		checked_cust: {},
		show_msgBox: false, // 在http请求方法中，登录过期的提示是否已弹出
		is_perfectInfo: true, // 进入“我的”判断是否弹窗完善个人信息的提示
	},
	getters:{
		accessToken: state => state.access_token,
		userInfo: state => state.user_info,
		vipInfo: state => state.vip_info,
		currentCity: state => state.current_city,
		checkedCust: state => state.checked_cust,
	},
	mutations: {
		setAccessToken(state, data){
			state.access_token = data;
		},
		setUserInfo(state, data){
			state.user_info = data;
		},
		setVipInfo(state, data){
			state.vip_info = data;
		},
		setUserProperty(state, data){
			state.user_property = data;
		},
		setCurrentCity(state, data){
			state.current_city = data;
		},
		setShowMsgBox(state, data){
			state.show_msgBox = data;
		},
		setIsPerfectInfo(state, data){
			state.is_perfectInfo = data;
		},
		setCheckedCust(state, data){
			state.checked_cust = data;
		},
	},
	actions: {
		wechatLogin({commit,dispatch}){
			return new Promise((resolve, reject) => {
				if(!this.state.access_token){
					uni.login({
						provider: 'weixin',
						success: loginRes => {
							http('post', '/auth/wxlogin', {loginCode: loginRes.code}, (data,msg) => {
								if (process.env.NODE_ENV === 'development') {
									uni.setStorageSync('access_token', data.access_token);
								}
								commit('setAccessToken', data.access_token);
								dispatch('getUserInfo');	
								resolve();					
							});	
						}
					})
				}else{
					resolve();
				}
			})
		},
		getUserInfo({commit}){
			return new Promise((resolve, reject) => {
				http('get', '/admin/user/info', {}, (data, msg) => {
					uni.setStorageSync('userInfo', data.sysUser);
					commit('setUserInfo', data.sysUser);
					commit('setUserProperty', data.property)
					resolve(data);
					if (data.sysUser.status == 0) {
						uni.showModal({
							title: '提示',
							content: '您的账号已被冻结，有疑问请联系客服',
						})
					}
				},msg => {
					reject(msg);
				})
			})			
		},
		getVipInfo({commit}){
			return new Promise((resolve, reject) => {
				http('get', '/nursing/oms/vip/my', {}, (data,msg) => {
					uni.setStorageSync('vipInfo', data);
					commit('setVipInfo', data);
					resolve(data);
				})
			},msg => {
				reject(msg);
			})
		},
		// 返回登录的上一页
		backFromLoginPage() {
			let before_login = uni.getStorageSync('before_login');
			let switchTab = [
				'/pages/index/index',
				'/pages/order/index',
				'/pages/community/index',
				'/pages/usercenter/index',
			];
			if (before_login) {
				if(switchTab.includes(before_login)){
					uni.switchTab({
						url: before_login
					})
				}else{
					uni.reLaunch({
						url: before_login
					});
				}
				uni.removeStorageSync('before_login');
			} else {
				uni.switchTab({
					url: '/pages/index/index'
				})
			}
		},
	}
})

export default store
